/*
 * Painel para fazer a configuração da conexão com o banco de dados Firebird
 */
package br.inf.acesso.view.conexao;

import br.inf.acesso.regradenegocios.bancodedados.Conexao;
import br.inf.acesso.regradenegocios.bancodedados.ArquivoConexao;
import br.inf.acesso.validadores.BancoDeDados;
import br.inf.acesso.validadores.CampoNumericoDocument;
import javax.swing.JOptionPane;

/**
 * @author Douglas
 * @since 28/04/2013
 */
public class PainelConexaoFirebird extends javax.swing.JPanel {

    ArquivoConexao arqFirebird;
    Conexao conexao;

    public PainelConexaoFirebird() {
        arqFirebird = new ArquivoConexao("C:\\nQuestorEmp\\Conexao", "ConexaoFirebird.txt");
        conexao = arqFirebird.buscaConexao();
        initComponents();
        atualizaCampos();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btTestaConexao = new javax.swing.JButton();
        txtSenha = new javax.swing.JLabel();
        txtUsuario = new javax.swing.JLabel();
        txtPorta = new javax.swing.JLabel();
        txtLocal = new javax.swing.JLabel();
        txtDatabase = new javax.swing.JLabel();
        campoSenha = new javax.swing.JPasswordField();
        btSalvar = new javax.swing.JButton();
        campoPorta = new javax.swing.JTextField();
        campoUsuario = new javax.swing.JTextField();
        campoDatabase = new javax.swing.JTextField();
        campoLocal = new javax.swing.JTextField();

        btTestaConexao.setText("Testar");
        btTestaConexao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btTestaConexaoActionPerformed(evt);
            }
        });

        txtSenha.setText("Senha");

        txtUsuario.setText("Usuario");

        txtPorta.setText("Porta");

        txtLocal.setText("Local");

        txtDatabase.setText("Database");

        btSalvar.setText("Salvar");
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });

        campoPorta.setToolTipText("");
        //impossibilita a digitação de informações diferente de numeros
        campoPorta.setDocument(new CampoNumericoDocument(true));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtPorta)
                    .addComponent(txtLocal)
                    .addComponent(txtDatabase)
                    .addComponent(txtUsuario)
                    .addComponent(txtSenha))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(btSalvar)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btTestaConexao))
                        .addComponent(campoLocal)
                        .addComponent(campoPorta)
                        .addComponent(campoUsuario)
                        .addComponent(campoSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(campoDatabase, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(28, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDatabase)
                    .addComponent(campoDatabase, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtLocal)
                    .addComponent(campoLocal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPorta)
                    .addComponent(campoPorta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtUsuario)
                    .addComponent(campoUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSenha)
                    .addComponent(campoSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btSalvar)
                    .addComponent(btTestaConexao))
                .addContainerGap(63, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btTestaConexaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btTestaConexaoActionPerformed
        atualizaVlConexao();
        if (arqFirebird.testaConexao("org.firebirdsql.jdbc.FirebirdDriver", conexao, BancoDeDados.Firebird)) {
            JOptionPane.showMessageDialog(null, "Conexão realizada com sucesso!");
        }
    }//GEN-LAST:event_btTestaConexaoActionPerformed

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
        atualizaVlConexao();
        if (arqFirebird.salvaConexao(conexao)) {
            JOptionPane.showMessageDialog(null, "Salvo com Sucesso!");
        }
    }//GEN-LAST:event_btSalvarActionPerformed
    public void atualizaVlConexao() {
        conexao.setLocal(campoLocal.getText());
        conexao.setBanco(campoDatabase.getText());
        conexao.setPorta(Integer.parseInt(campoPorta.getText()));
        conexao.setUsuario(campoUsuario.getText());
        conexao.setSenha(campoSenha.getText());
    }

    public void atualizaCampos() {
        campoDatabase.setText(conexao.getBanco());
        campoLocal.setText(conexao.getLocal());
        campoPorta.setText(conexao.getPorta() + "");
        campoUsuario.setText(conexao.getUsuario());
        campoSenha.setText(conexao.getSenha());
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btSalvar;
    private javax.swing.JButton btTestaConexao;
    private javax.swing.JTextField campoDatabase;
    private javax.swing.JTextField campoLocal;
    private javax.swing.JTextField campoPorta;
    private javax.swing.JPasswordField campoSenha;
    private javax.swing.JTextField campoUsuario;
    private javax.swing.JLabel txtDatabase;
    private javax.swing.JLabel txtLocal;
    private javax.swing.JLabel txtPorta;
    private javax.swing.JLabel txtSenha;
    private javax.swing.JLabel txtUsuario;
    // End of variables declaration//GEN-END:variables
}
